package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.context.BaseContext;
import com.example.demo.dto.ElderlyBasicAddDTO;
import com.example.demo.dto.PageDTO;
import com.example.demo.mapper.ElderlyBasicMapper;
import com.example.demo.pojo.ElderlyBasic;
import com.example.demo.pojo.ElderlyDetailed;
import com.example.demo.service.ElderlyBasicService;
import com.example.demo.service.ElderlyDetailedService;
import com.example.demo.utils.RedisIdWorker;
import com.example.demo.vo.ElderlyVO;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ElderlyBasicServiceImpl extends ServiceImpl<ElderlyBasicMapper, ElderlyBasic> implements ElderlyBasicService {

    @Resource
    private RedisIdWorker redisIdWorker;

    @Override
    public void addElderlyBasic(ElderlyBasicAddDTO elderlyBasicAddDTO) {
        ElderlyBasic elderlyBasicInfo = BeanUtil.copyProperties(elderlyBasicAddDTO, ElderlyBasic.class);
        elderlyBasicInfo.setId(redisIdWorker.nextId("elderly"));
        elderlyBasicInfo.setUserId(BaseContext.getCurrentId());
        save(elderlyBasicInfo);
    }

    @Override
    public List<ElderlyBasic> getElderly(PageDTO pageDTO) {
        Integer page = pageDTO.getPage();
        Integer pageSize = pageDTO.getPageSize();

        Page<ElderlyBasic> p = Page.of(page, pageSize);
        p.addOrder(new OrderItem("id",false));
        lambdaQuery().page(p);

        return p.getRecords();
    }
}
